package cn.ivicar.sim.controller;

import cn.com.carsmart.exception.CarsmartException;
import cn.com.carsmart.ws.model.SimFlowDailyStat;
import cn.com.carsmart.ws.model.SimFlowInfo;
import cn.com.carsmart.ws.model.SimFlowMonthlyStat;
import cn.com.carsmart.ws.model.SimInfo;
import cn.ivicar.sim.domain.FlowStatQuery;
import cn.ivicar.sim.domain.SimInfoQuery;
import cn.ivicar.sim.service.SimRpcService;
import cn.ivicar.sim.service.SimService;
import cn.ivicar.sim.util.ConvertUtils;
import cn.ivicar.sim.util.DateUtil;
import com.google.inject.Inject;
import net.sf.json.JSONObject;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * Created by lixiaozhuo on 17/5/14.
 */
public class SimWorkerController {
    @Inject
    private SimService simService;

    public void updateSimInfo(){
        SimInfoQuery query = new SimInfoQuery();
        int page = 0;
        int pageSize = 20;
        query.setPage(page);
        query.setPageSize(pageSize);

        List<SimInfo> simInfoList;
        do{
            simInfoList = simService.getSimInfoList(query);
            if(CollectionUtils.isNotEmpty(simInfoList)){
                for(SimInfo simInfo : simInfoList){
                    updateSingleSimInfo(simInfo);
                }
            }
        }while(CollectionUtils.isNotEmpty(simInfoList) && simInfoList.size() == pageSize);
    }

    private void updateSingleSimInfo(SimInfo simInfo){
        JSONObject json = SimRpcService.getSimInfo(simInfo.getIccid());
        JSONObject jsonSimStatus = SimRpcService.getSimStatusRealtime(simInfo.getIccid());
        JSONObject jsonGPRSStatus = SimRpcService.getGPRSStatusRealtime(simInfo.getIccid());
        JSONObject jsonSwitchStatus = SimRpcService.getSwitchStatusRealtime(simInfo.getIccid());
        SimInfo latestSimInfo = ConvertUtils.convertSimInfo(json, jsonSimStatus, jsonGPRSStatus, jsonSwitchStatus);
        if(latestSimInfo == null){
            return;
        }

        latestSimInfo.setId(simInfo.getId());
        latestSimInfo.setCreated(simInfo.getCreated());
        latestSimInfo.setModified(new Date());
        simService.updateSimInfo(latestSimInfo);
    }

    public void updateSimFlowInfo(){
        SimInfoQuery query = new SimInfoQuery();
        int page = 0;
        int pageSize = 20;
        query.setPage(page);
        query.setPageSize(pageSize);

        List<SimInfo> simInfoList;
        do{
            simInfoList = simService.getSimInfoList(query);
            if(CollectionUtils.isNotEmpty(simInfoList)){
                for(SimInfo simInfo : simInfoList){
                    updateSingleSimFlowInfo(simInfo.getIccid());
                }
            }
        }while(CollectionUtils.isNotEmpty(simInfoList) && simInfoList.size() == pageSize);
    }

//    public static void main(String[] args) {
//        String iccid = "898607B1101730308339";
//        JSONObject json = SimRpcService.getMonthlyFlowUsedRealtime(iccid);
//        System.out.println(json);
//        SimFlowInfo latestSimFlowInfo = ConvertUtils.convertSimFlowInfo(json);
//        System.out.println(latestSimFlowInfo);
//    }

    private void updateSingleSimFlowInfo(SimFlowInfo simFlowInfo){
        JSONObject json = SimRpcService.getMonthlyFlowUsedRealtime(simFlowInfo.getIccid());
        SimFlowInfo latestSimFlowInfo = ConvertUtils.convertSimFlowInfo(json);
        if(latestSimFlowInfo == null){
            return;
        }

        latestSimFlowInfo.setId(simFlowInfo.getId());
        latestSimFlowInfo.setCreated(simFlowInfo.getCreated());
        simService.updateSimFlowInfo(latestSimFlowInfo);
    }

    private void updateSingleSimFlowInfo(String iccid) {
        JSONObject json = SimRpcService.getMonthlyFlowUsedRealtime(iccid);
        SimFlowInfo latestSimFlowInfo = ConvertUtils.convertSimFlowInfo(json);
        if (latestSimFlowInfo == null) {
            return;
        }

        latestSimFlowInfo.setIccid(iccid);
        SimFlowInfo simFlowInfo = simService.getSimFlowInfoByIccid(iccid);
        if (simFlowInfo != null) {
            simService.updateSimFlowInfo(latestSimFlowInfo);
        } else {
            simService.insertSimFlowInfo(latestSimFlowInfo);
        }
    }

    public void updateFlowDailyStat() throws CarsmartException {
        Date occurDate = DateUtil.addDays(new Date(), -1);
        updateFlowDailyStat(null, occurDate);
    }

    public void updateFlowDailyStat(String iccid, Date occurDate) throws CarsmartException {
        if(StringUtils.isNotBlank(iccid)){
            updateSingleFlowDailyStat(iccid, occurDate);
            return;
        }

        SimInfoQuery query = new SimInfoQuery();
        int page = 0;
        int pageSize = 20;
        query.setPage(page);
        query.setPageSize(pageSize);

        List<SimInfo> simInfoList;
        do{
            simInfoList = simService.getSimInfoList(query);
            if(CollectionUtils.isNotEmpty(simInfoList)){
                for(SimInfo simInfo : simInfoList){
                    updateSingleFlowDailyStat(simInfo.getIccid(), occurDate);
                }
            }
        }while(CollectionUtils.isNotEmpty(simInfoList) && simInfoList.size() == pageSize);
    }

    private void updateSingleFlowDailyStat(String iccid, Date occurDate) throws CarsmartException {
        String strOccurDate = DateUtil.DATEFORMATER2().format(occurDate);
        JSONObject json = SimRpcService.getDailyFlowUsed(iccid, strOccurDate);
        if (json == null || !ConvertUtils.SUCCESS_CODE.equals(json.opt(ConvertUtils.KEY_RETURN_CODE))) {
            return;
        }

        long dayFlow = json.getJSONArray("result").getJSONObject(0).optInt("current_flow");

        Date now = new Date();
        int date = Integer.parseInt(DateUtil.DATEFORMATER().format(occurDate));
        FlowStatQuery query = new FlowStatQuery();
        query.setIccid(iccid);
        query.setStartDate(date);
        query.setEndDate(date);
        query.setPage(0);
        query.setPageSize(10);
        List<SimFlowDailyStat> flowDailyStatList = simService.getFlowDailyStatList(query);
        SimFlowDailyStat simFlowDailyStat;
        if(CollectionUtils.isNotEmpty(flowDailyStatList)){
            simFlowDailyStat = flowDailyStatList.get(0);
            simFlowDailyStat.setFlowUsed(dayFlow);
            simFlowDailyStat.setModified(now);
            simService.updateFlowDailyStat(simFlowDailyStat);
        }else{
            simFlowDailyStat = new SimFlowDailyStat();
            simFlowDailyStat.setIccid(iccid);
            simFlowDailyStat.setFlowUsed(dayFlow);
            simFlowDailyStat.setOccurDate(date);
            simFlowDailyStat.setCreated(now);
            simFlowDailyStat.setModified(now);
            simService.insertFlowDailyStat(simFlowDailyStat);
        }
    }

    public static void main(String[] args) {
        String iccid = "898607B1101730308339";
        String strOccurDate = "2017-08-12";
        JSONObject json = SimRpcService.getDailyFlowUsed(iccid, strOccurDate);
        if (json == null || !ConvertUtils.SUCCESS_CODE.equals(json.opt(ConvertUtils.KEY_RETURN_CODE))) {
            return;
        }

        long dayFlow = json.getJSONArray("result").getJSONObject(0).optInt("current_flow");
        System.out.println(dayFlow);
    }

//    private void updateSingleFlowDailyStat(String iccid) throws CarsmartException {
//        JSONObject json = SimRpcService.getDailyFlowUsed(iccid);
//        if(json == null || !json.containsKey("dayFlow") || !json.containsKey("syncDate")){
//            return;
//        }
//
//        long dayFlow = json.getLong("dayFlow");
//        String strOccurDate = json.getString("syncDate");
//        Date occurDate = DateUtil.formatTime1(strOccurDate);
//
//        int date = Integer.parseInt(DateUtil.DATEFORMATER().format(occurDate));
//        FlowStatQuery query = new FlowStatQuery();
//        query.setIccid(iccid);
//        query.setStartDate(date);
//        query.setEndDate(date);
//        query.setPage(0);
//        query.setPageSize(10);
//        List<SimFlowDailyStat> flowDailyStatList = simService.getFlowDailyStatList(query);
//        SimFlowDailyStat simFlowDailyStat;
//        if(CollectionUtils.isNotEmpty(flowDailyStatList)){
//            simFlowDailyStat = flowDailyStatList.get(0);
//            simFlowDailyStat.setFlowUsed(dayFlow);
//            simService.updateFlowDailyStat(simFlowDailyStat);
//        }else{
//            simFlowDailyStat = new SimFlowDailyStat();
//            simFlowDailyStat.setIccid(iccid);
//            simFlowDailyStat.setFlowUsed(dayFlow);
//            simFlowDailyStat.setOccurDate(date);
//            simFlowDailyStat.setCreated(new Date());
//            simFlowDailyStat.setModified(simFlowDailyStat.getCreated());
//            simService.insertFlowDailyStat(simFlowDailyStat);
//        }
//    }

    public void updateFlowMonthlyStat() throws CarsmartException {
        updateFlowMonthlyStat(null);
    }

    public void updateFlowMonthlyStat(String iccid) throws CarsmartException {
        if(StringUtils.isNotBlank(iccid)){
            updateSingleFlowMonthlyStat(iccid);
            return;
        }

        SimInfoQuery query = new SimInfoQuery();
        int page = 0;
        int pageSize = 20;
        query.setPage(page);
        query.setPageSize(pageSize);

        List<SimInfo> simInfoList;
        do{
            simInfoList = simService.getSimInfoList(query);
            if(CollectionUtils.isNotEmpty(simInfoList)){
                for(SimInfo simInfo : simInfoList){
                    updateSingleFlowMonthlyStat(simInfo.getIccid());
                }
            }
        }while(CollectionUtils.isNotEmpty(simInfoList) && simInfoList.size() == pageSize);
    }

//    private void updateSingleFlowMonthlyStat(String iccid){
//        Date now = new Date();
//        Date firstDay = DateUtil.getFirstDayOfMonth(now);
//        Date lastDay = DateUtil.addDays(DateUtil.addMonths(firstDay, 1), -1);
//        int startDate = Integer.parseInt(DateUtil.DATEFORMATER().format(firstDay));
//        int endDate = Integer.parseInt(DateUtil.DATEFORMATER().format(lastDay));
//        int occurMonth = Integer.parseInt(DateUtil.MONTHFORMATER().format(DateUtil.formatMonth(now)));
//        FlowStatQuery query = new FlowStatQuery();
//        query.setIccid(iccid);
//        query.setStartDate(startDate);
//        query.setEndDate(endDate);
//        query.setPage(0);
//        query.setPageSize(10);
//        List<SimFlowDailyStat> flowDailyStatList = simService.getFlowDailyStatList(query);
//        if(CollectionUtils.isEmpty(flowDailyStatList)){
//            return;
//        }
//
//        long flowUsed = 0L;
//        for(SimFlowDailyStat simFlowDailyStat : flowDailyStatList){
//            flowUsed += simFlowDailyStat.getFlowUsed();
//        }
//
//        SimFlowMonthlyStat simFlowMonthlyStat;
//        query.setStartDate(occurMonth);
//        query.setEndDate(occurMonth);
//        List<SimFlowMonthlyStat> flowMonthlyStatList = simService.getFlowMonthlyStatList(query);
//        if(CollectionUtils.isNotEmpty(flowMonthlyStatList)){
//            simFlowMonthlyStat = flowMonthlyStatList.get(0);
//            simFlowMonthlyStat.setFlowUsed(flowUsed);
//            simFlowMonthlyStat.setModified(now);
//            simService.updateFlowMonthlyStat(simFlowMonthlyStat);
//        }else{
//            simFlowMonthlyStat = new SimFlowMonthlyStat();
//            simFlowMonthlyStat.setIccid(iccid);
//            simFlowMonthlyStat.setFlowUsed(flowUsed);
//            simFlowMonthlyStat.setOccurMonth(occurMonth);
//            simFlowMonthlyStat.setCreated(now);
//            simFlowMonthlyStat.setModified(now);
//            simService.insertFlowMonthlyStat(simFlowMonthlyStat);
//        }
//    }

    private void updateSingleFlowMonthlyStat(String iccid){
        JSONObject json = SimRpcService.getMonthlyFlowUsedRealtime(iccid);
        if (json == null || !ConvertUtils.SUCCESS_CODE.equals(json.opt(ConvertUtils.KEY_RETURN_CODE))) {
            return;
        }

        long flowUsed = json.getJSONObject("result").optLong("flow_spend");

        Date now = new Date();
        int occurMonth = Integer.parseInt(DateUtil.MONTHFORMATER().format(DateUtil.formatMonth(now)));
        FlowStatQuery query = new FlowStatQuery();
        query.setIccid(iccid);
        query.setPage(0);
        query.setPageSize(10);

        SimFlowMonthlyStat simFlowMonthlyStat;
        query.setStartDate(occurMonth);
        query.setEndDate(occurMonth);
        List<SimFlowMonthlyStat> flowMonthlyStatList = simService.getFlowMonthlyStatList(query);
        if(CollectionUtils.isNotEmpty(flowMonthlyStatList)){
            simFlowMonthlyStat = flowMonthlyStatList.get(0);
            simFlowMonthlyStat.setFlowUsed(flowUsed);
            simFlowMonthlyStat.setModified(now);
            simService.updateFlowMonthlyStat(simFlowMonthlyStat);
        }else{
            simFlowMonthlyStat = new SimFlowMonthlyStat();
            simFlowMonthlyStat.setIccid(iccid);
            simFlowMonthlyStat.setFlowUsed(flowUsed);
            simFlowMonthlyStat.setOccurMonth(occurMonth);
            simFlowMonthlyStat.setCreated(now);
            simFlowMonthlyStat.setModified(now);
            simService.insertFlowMonthlyStat(simFlowMonthlyStat);
        }
    }
}
